const app = getApp()
//const utils = require('../../pages/utils/utils.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(app.globalData.orderList)
  },
  //   console.log(app.globalData.cartList)
  //   this.setData({
  //     cartList:app.globalData.cartList
  //   })
  //   //计算合计
  //   this.total()
  // },
  onShow(){
    console.log(app.globalData.cartList)
    this.setData({
      cartList:app.globalData.cartList
    })
    //计算合计
    this.total()
  },
  //添加
  add(event){
    console.log(event.currentTarget.dataset.index)
    let index = event.currentTarget.dataset.index
    this.data.cartList[index].number = this.data.cartList[index].number+1
    this.setData({
      cartList:this.data.cartList
    })
    //更新全局里和缓存里的购物车列表数据
    app.globalData.cartList=this.data.cartList
    wx.setStorageSync('cartList', this.data.cartList)
    //计算合计
    this.total()
},
  //减少
  reduce(event){
    console.log(event.currentTarget.dataset.index)
    let index = event.currentTarget.dataset.index
    if(this.data.cartList[index].number!=1){
      this.data.cartList[index].number = this.data.cartList[index].number-1
      this.setData({
        cartList:this.data.cartList
      })
      //更新全局里和缓存里的购物车列表数据
    app.globalData.cartList=this.data.cartList
    wx.setStorageSync('cartList', this.data.cartList)
    //计算合计
    this.total()
    }else{
      wx.showModal({
        title: '提示',
        content: '确认不购买此商品吗',
        confirmText:'确定'
      })
    .then(res=>{
      if(res.confirm == true){
    this.data.cartList.splice(index,1)
    this.setData({
      cartList:this.data.cartList
    })
    //更新全局里和缓存里的购物车列表数据
    app.globalData.cartList=this.data.cartList
    wx.setStorageSync('cartList', this.data.cartList)
    //计算合计
    this.total()
    }
  })
}
},
toGoodDetail(event){
  console.log(event.currentTarget.dataset.id)
    let id = event.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/goodDetail/goodDetail?id='+id,
})
},
//全选
chooseAll(){
  this.setData({
    allChoose:!this.data.allChoose
  })
  if(this.data.allChoose == true){
    for(let index in this.data.cartList){
      this.data.cartList[index].choose = true
    }
  }else{
    for(let index in this.data.cartList){
      this.data.cartList[index].choose =false
    }
  } 
  this.setData({
    cartList:this.data.cartList
  })
  //更新全局里和缓存里的购物车列表数据
  app.globalData.cartList=this.data.cartList
  wx.setStorageSync('cartList', this.data.cartList)
 //计算合计
  this.total()
},
// chooseGood(event){
//   console.log(event.currentTarget.dataset.index)
//   let index = event.currentTarget.dataset.index
//   this.data.cartList[index].choose = !this.data.cartList[index].choose 
//   this.setData({
//     cartList:this.data.cartList
//   })
//   //更新全局里和缓存里的购物车列表数据
//   app.globalData.cartList=this.data.cartList
//   wx.setStorageSync('cartList', this.data.cartList)
//   //计算合计
//   this.total() 
// },
//计算合计价格
total(){
  let sum = 0;
  let totalNumber = 0
  for(let index in this.data.cartList){
    if(this.data.cartList[index].choose == true){
      sum = sum + this.data.cartList[index].price * this.data.cartList[index].number
      totalNumber = totalNumber+this.data.cartList[index].number
    }
  }
  this.setData({
   sum:sum.toFixed(2),
   totalNumber
  })
  
},
   getNode(event){
     console.log(event.detail.value)

},

addOrder(){
  wx.cloud.database().collection('shop_orders').add({
    data:{goods:this.data.cartList,
    totalMoney:this.data.sum,
    note:this.data.note,
    status:-1
    //-2：取消支付
    //-1：待支付
    //1：制作中
    //2已完成
    }
  }).then(res=>{
    console.log(res._id)
    wx.showToast({
      title: '提交成功',
      })
      let orderId = res._id
       this.setData({
         orderId
       })
      //调用起虚拟支付
      this.xuniPay()
  
    })

}, 
xuniPay(){
     let that = this
      wx.showModal({
        title:'提示',
        content:'是否支付商品价格'+this.data.sum+'元',
        confirmText:'支付'
      })
      .then(res=>{
        console.log(res)
        if(res.confirm == true){
          wx.cloud.database().collection('shop_orders').doc(this.data.orderId)
          .update({
            data:{
            status:1
            }
          })
          .then(result=>{
          console.log(result)
          //从购物车清除订单里边的商品
          that.clearCarList()
          wx.navigateBack({
            delta:0,
            success(){
              // wx.request({
              //   url: '127.0.0.1',
              // })
              wx.showToast({
                title: '支付成功',
              })
            }
           
          })
          })
          wx.request({
            url: 'http://47.115.214.192', //仅为示例，并非真实的接口地址
            method:'POST',
            data: {
              msg: '重量',
              token: '10'
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success (res) {
              console.log(res.data)
            }
          })
        }else{
          wx.navigateBack({
            delta:0,
            success(){
              wx.showToast({
                icon:'error',
                title: '支付失败',
              })
          }
          })

        }
      })
    //   wx.request({
    //     url: 'http://47.115.214.192', //仅为示例，并非真实的接口地址
    //     method:'POST',
    //     data: {
    //       msg: '重量',
    //       token: '10'
    //     },
    //     header: {
    //       'content-type': 'application/json' // 默认值
    //     },
    //     success (res) {
    //       console.log(res.data)
    //     }
    //   })
     },
    clearCarList(){
      for (let  i in app.globalData.cartList){
        for(let j in app.globalData.orderList){
          if(app.globalData.orderList[j]._id == app.globalData.cartList[i]._id){
          app.globalData.cartList.splice(i,1)
          }
        }
      }
      wx.setStorageSync('cartList', app.globalData.cartList)
    },
      //分享给好友 
onShareAppMessage(){
  return{
    title:this.data.good.title,
    path:'/pages/goodDetail/goodDetail='+this.data.good._id,
    imageUrl:this.data.good.cover
  }
},
//分享到朋友圈
onShareTimeline(){
  return {
    title:this.data.good.title,
    query:{
      id:this.data.good._id
  },
  imageUrl:this.data.good.cover
}
},


    pay(){
      
    },
  })
    // data.cartList[index])
    //     }
    //   }
      // app.globalData.orderList = orderList
      // if(app.globalData.orderList.length == 0){
      //   wx.showToast({
      //     icon:'error',
      //     title: '请选择商品',
      //   })
      //   return
      // }
      // wx.navigateTo({
      //   url: '/pages/order2/order2',
    
    //   })toOrder(){
    //   let orderList = []
    //   for(let index in this.data.cartList){
    //     if(this.data.cartList[index].choose == true){
    //       orderList.push(this.
    // },
    // addAddress(){
    //   wx.chooseAddress({
    //     success:(result) =>{
    //       console.log(result)
    //     }
    //   })
    // },
//})

// }
// toOrder(){
  
//   wx.navigateTo({
//     url: '/pages/order2/order2',
//   })
//   let orderId = res._id
//   //调用起微信支付
//   this.pay()
//   //调用起虚拟支付
//   this.xuniPay()
// },
//   xuniPay(){
//     wx.showModal({
//       title:'提示',
//       content:'是否支付商品价格'+this.data.sum+'元',
//       confirmText:'支付'
//     })
//     .then(res=>{
//       console.log(res)
//     })
//   },
//   pay(){

//   }
  
// })